package com.gxwzxy.medic.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.gxwzxy.medic.entity.Menu;
import com.gxwzxy.medic.entity.Role;
import com.gxwzxy.medic.entity.otherentity.MenuTree;
import com.gxwzxy.medic.service.MenuService;
import com.gxwzxy.medic.service.Impl.MenuServiceImpl;
import com.gxwzxy.medic.until.PageBean;
import com.my.web.servlet.RequestBeanUtils;

/**
 * Servlet implementation class MenuServlet
 */
public class MenuServlet extends BaseServlet {
	private static final long serialVersionUID = 1L;
	
	/**
	 * 鏉冮檺鍒嗛〉
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws ServletException
	 */
	public void findMenu(HttpServletRequest request,HttpServletResponse response)throws IOException,ServletException{
	
		//获取前端传来的值
		String sindex = request.getParameter("index");
    	int index = 1;
		try {
    		index = Integer.parseInt(sindex);
    	}catch(NumberFormatException n) {
    		n.printStackTrace();
    	}
    	PageBean<Menu> pu = new PageBean<Menu>();
    	pu.setIndex(index);//设置需要查询数据的起始坐标
    	MenuService ms = new MenuServiceImpl();
    	ms.pageMenu(pu);
		request.setAttribute("pu", pu);
		request.getRequestDispatcher("page/menu/menu.jsp").forward(request, response);
	}

	/**
	 * 查询全部的菜单数据
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws ServletException
	 */
	public void findAllMenu(HttpServletRequest request,HttpServletResponse response)throws IOException,ServletException{
		MenuService ms = new MenuServiceImpl();
		List<Menu> lm = ms.findMenu();
		response.getWriter().write(new Gson().toJson(lm));
	}
	
	/**
	 * 增加一个菜单
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws ServletException
	 */
	public void addMenu(HttpServletRequest request,HttpServletResponse response)throws IOException,ServletException{
		//获取前端的二级菜单
		String menupId = request.getParameter("secondpId");
		//如果发现二级菜单为空
		if(menupId.equals("0")) {
			//
			menupId = request.getParameter("menupId");
		}
		String menuId = request.getParameter("menuId");
		String menuName = request.getParameter("menuName");
		String url = request.getParameter("url");
		//澶勭悊璇锋眰
		Menu m = new Menu();
		m.setMenupId(menupId);
		m.setMenuId(menuId);
		m.setMenuName(menuName);
		m.setUrl(url);
		MenuService ms = new MenuServiceImpl();
		boolean bl = ms.addMenu(m);
		if(bl) {
			request.setAttribute("url", "MenuServlet?method=findMenu&&index=1");
			request.getRequestDispatcher("tips.jsp").forward(request, response);
		}
	}
	
	/**
	 * 查询单条menu的记录
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws ServletException
	 */
	public void findMenuBymenuId(HttpServletRequest request,HttpServletResponse response)throws IOException,ServletException{
		//获得前端传来数据，并封装成实体类
		Menu m = RequestBeanUtils.requestToBean(request, Menu.class);
		//获得查询的service
		MenuService ms = new MenuServiceImpl();
		List<Menu> lm = ms.findMenuBymenuId(m.getMenuId());//根据menuId查询到具体的某一条数据
		List<Menu> alm = ms.findMenu();//查询得到全部的menu数据
		request.setAttribute("lm", lm);
		request.setAttribute("alm", alm);
		request.getRequestDispatcher("page/menu/updateMenu.jsp").forward(request, response);
	}
	
	/**
	 * 鏍规嵁menuId淇敼鏉冮檺
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws ServletException
	 */
	public void updateMenu(HttpServletRequest request,HttpServletResponse response)throws IOException,ServletException{
		//鑾峰彇璇锋眰
		Menu m = RequestBeanUtils.requestToBean(request, Menu.class);
		String rmenuId = request.getParameter("rmenuId");
		//澶勭悊璇锋眰
		MenuService ms = new MenuServiceImpl();
		boolean bl = ms.updateMenu(m,rmenuId);
		if(bl) {
			request.setAttribute("url", "MenuServlet?method=findMenu&&index=1");
			request.getRequestDispatcher("tips.jsp").forward(request, response);
		}
	}
	/**
	 * 根据菜单编号激进行删除某一条菜单数据
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws ServletException
	 */
	public void MenuDelete(HttpServletRequest request,HttpServletResponse response)throws IOException,ServletException{
		//构造实体类
		Menu m = RequestBeanUtils.requestToBean(request, Menu.class);
		//进行删除
		MenuService ms = new MenuServiceImpl();
		boolean bl = ms.MenuDelete(m.getMenuId());
		if(bl) {
			response.getWriter().write("true");
		}else {
			response.getWriter().write("false");
		}
	}
	/**
	 * 根据roleId查询该角色所拥有的的菜单选项并返回jsp页面，json格式
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws ServletException
	 */
	public void findMenuByroleId(HttpServletRequest request,HttpServletResponse response)throws IOException,ServletException{
		//构造bean
		Role r = RequestBeanUtils.requestToBean(request, Role.class);
		//查询该角色下所有的菜单选项
		MenuService ms = new MenuServiceImpl();
		List<MenuTree> lm = ms.findMenuByroleId(r.getRoleId());
		response.getWriter().write(new Gson().toJson(lm));
	}
	
	/**
	 * 根据menupId查询二级菜单
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws ServletException
	 */
	public void findSecondMenu(HttpServletRequest request,HttpServletResponse response)throws IOException,ServletException{
		//获取前端传来的值
		String menupId = request.getParameter("menupId");
		//查询
		MenuService ms= new MenuServiceImpl();
		List<Menu> lm = ms.findSecondMenu(menupId);
		response.getWriter().write(new Gson().toJson(lm));
	}
}
